home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / examples / evaltest.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  11KB  |  516 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1996. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /* This program was originally written by someone else (Simon Hui?);
  9.    I just added a bit more GLUT stuff to it. */
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <math.h>
  15. #include <GL/glut.h>
  16.  
  17. #define VORDER 10
  18. #define CORDER 10
  19. #define TORDER 3
  20.  
  21. #define VMAJOR_ORDER 2
  22. #define VMINOR_ORDER 3
  23.  
  24. #define CMAJOR_ORDER 2
  25. #define CMINOR_ORDER 2
  26.  
  27. #define TMAJOR_ORDER 2
  28. #define TMINOR_ORDER 2
  29.  
  30. #define VDIM 4
  31. #define CDIM 4
  32. #define TDIM 2
  33.  
  34. #define ONE_D 1
  35. #define TWO_D 2
  36.  
  37. #define EVAL 3
  38. #define MESH 4
  39.  
  40. GLenum doubleBuffer;
  41.  
  42. float rotX = 0.0, rotY = 0.0, translateZ = -1.0;
  43.  
  44. GLenum arrayType = ONE_D;
  45. GLenum colorType = GL_FALSE;
  46. GLenum textureType = GL_FALSE;
  47. GLenum polygonFilled = GL_FALSE;
  48. GLenum lighting = GL_FALSE;
  49. GLenum mapPoint = GL_FALSE;
  50. GLenum mapType = EVAL;
  51.  
  52. double point1[10 * 4] =
  53. {
  54.   -0.5, 0.0, 0.0, 1.0,
  55.   -0.4, 0.5, 0.0, 1.0,
  56.   -0.3, -0.5, 0.0, 1.0,
  57.   -0.2, 0.5, 0.0, 1.0,
  58.   -0.1, -0.5, 0.0, 1.0,
  59.   0.0, 0.5, 0.0, 1.0,
  60.   0.1, -0.5, 0.0, 1.0,
  61.   0.2, 0.5, 0.0, 1.0,
  62.   0.3, -0.5, 0.0, 1.0,
  63.   0.4, 0.0, 0.0, 1.0,
  64. };
  65. double cpoint1[10 * 4] =
  66. {
  67.   0.0, 0.0, 1.0, 1.0,
  68.   0.3, 0.0, 0.7, 1.0,
  69.   0.6, 0.0, 0.3, 1.0,
  70.   1.0, 0.0, 0.0, 1.0,
  71.   1.0, 0.3, 0.0, 1.0,
  72.   1.0, 0.6, 0.0, 1.0,
  73.   1.0, 1.0, 0.0, 1.0,
  74.   1.0, 1.0, 0.5, 1.0,
  75.   1.0, 1.0, 1.0, 1.0,
  76. };
  77. double tpoint1[11 * 4] =
  78. {
  79.   0.0, 0.0, 0.0, 1.0,
  80.   0.0, 0.1, 0.0, 1.0,
  81.   0.0, 0.2, 0.0, 1.0,
  82.   0.0, 0.3, 0.0, 1.0,
  83.   0.0, 0.4, 0.0, 1.0,
  84.   0.0, 0.5, 0.0, 1.0,
  85.   0.0, 0.6, 0.0, 1.0,
  86.   0.0, 0.7, 0.0, 1.0,
  87.   0.0, 0.8, 0.0, 1.0,
  88.   0.0, 0.9, 0.0, 1.0,
  89. };
  90. double point2[2 * 3 * 4] =
  91. {
  92.   -0.5, -0.5, 0.5, 1.0,
  93.   0.0, 1.0, 0.5, 1.0,
  94.   0.5, -0.5, 0.5, 1.0,
  95.   -0.5, 0.5, -0.5, 1.0,
  96.   0.0, -1.0, -0.5, 1.0,
  97.   0.5, 0.5, -0.5, 1.0,
  98. };
  99. double cpoint2[2 * 2 * 4] =
  100. {
  101.   0.0, 0.0, 0.0, 1.0,
  102.   0.0, 0.0, 1.0, 1.0,
  103.   0.0, 1.0, 0.0, 1.0,
  104.   1.0, 1.0, 1.0, 1.0,
  105. };
  106. double tpoint2[2 * 2 * 2] =
  107. {
  108.   0.0, 0.0, 0.0, 1.0,
  109.   1.0, 0.0, 1.0, 1.0,
  110. };
  111. float textureImage[4 * 2 * 4] =
  112. {
  113.   1.0, 1.0, 1.0, 1.0,
  114.   1.0, 0.0, 0.0, 1.0,
  115.   1.0, 0.0, 0.0, 1.0,
  116.   1.0, 1.0, 1.0, 1.0,
  117.   1.0, 1.0, 1.0, 1.0,
  118.   1.0, 0.0, 0.0, 1.0,
  119.   1.0, 0.0, 0.0, 1.0,
  120.   1.0, 1.0, 1.0, 1.0,
  121. };
  122.  
  123. static void
  124. Init(void)
  125. {
  126.   static float ambient[] =
  127.   {0.1, 0.1, 0.1, 1.0};
  128.   static float diffuse[] =
  129.   {1.0, 1.0, 1.0, 1.0};
  130.   static float position[] =
  131.   {0.0, 0.0, -150.0, 0.0};
  132.   static float front_mat_shininess[] =
  133.   {30.0};
  134.   static float front_mat_specular[] =
  135.   {0.7, 0.7, 0.7, 1.0};
  136.   static float front_mat_diffuse[] =
  137.   {1.0, 0.2, 1.0, 1.0};
  138.   static float back_mat_shininess[] =
  139.   {50.0};
  140.   static float back_mat_specular[] =
  141.   {0.5, 0.5, 0.2, 1.0};
  142.   static float back_mat_diffuse[] =
  143.   {1.0, 1.0, 0.2, 1.0};
  144.   static float lmodel_ambient[] =
  145.   {1.0, 1.0, 1.0, 1.0};
  146.   static float lmodel_twoside[] =
  147.   {GL_TRUE};
  148.   static float decal[] =
  149.   {GL_DECAL};
  150.   static float modulate[] =
  151.   {GL_MODULATE};
  152.   static float repeat[] =
  153.   {GL_REPEAT};
  154.   static float clamp[] =
  155.   {GL_CLAMP};
  156.   static float nr[] =
  157.   {GL_NEAREST};
  158.   static float ln[] =
  159.   {GL_LINEAR};
  160.  
  161.   glFrontFace(GL_CCW);
  162.  
  163.   glEnable(GL_DEPTH_TEST);
  164.  
  165.   glMap1d(GL_MAP1_VERTEX_4, 0.0, 1.0, VDIM, VORDER, point1);
  166.   glMap1d(GL_MAP1_COLOR_4, 0.0, 1.0, CDIM, CORDER, cpoint1);
  167.  
  168.   glMap2d(GL_MAP2_VERTEX_4, 0.0, 1.0, VMINOR_ORDER * VDIM, VMAJOR_ORDER, 0.0,
  169.     1.0, VDIM, VMINOR_ORDER, point2);
  170.   glMap2d(GL_MAP2_COLOR_4, 0.0, 1.0, CMINOR_ORDER * CDIM, CMAJOR_ORDER, 0.0,
  171.     1.0, CDIM, CMINOR_ORDER, cpoint2);
  172.   glMap2d(GL_MAP2_TEXTURE_COORD_2, 0.0, 1.0, TMINOR_ORDER * TDIM,
  173.     TMAJOR_ORDER, 0.0, 1.0, TDIM, TMINOR_ORDER, tpoint2);
  174.  
  175.   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  176.   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  177.   glLightfv(GL_LIGHT0, GL_POSITION, position);
  178.  
  179.   glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
  180.   glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
  181.  
  182.   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  183.   glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  184.  
  185.   glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal);
  186.   glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeat);
  187.   glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeat);
  188.   glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, nr);
  189.   glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, nr);
  190.   glTexImage2D(GL_TEXTURE_2D, 0, 4, 2, 4, 0, GL_RGBA, GL_FLOAT,
  191.     (GLvoid *) textureImage);
  192. }
  193.  
  194. static void
  195. DrawPoints1(void)
  196. {
  197.   GLint i;
  198.  
  199.   glColor3f(0.0, 1.0, 0.0);
  200.   glPointSize(2);
  201.   glBegin(GL_POINTS);
  202.   for (i = 0; i < VORDER; i++) {
  203.     glVertex4dv(&point1[i * 4]);
  204.   }
  205.   glEnd();
  206. }
  207.  
  208. static void
  209. DrawPoints2(void)
  210. {
  211.   GLint i, j;
  212.  
  213.   glColor3f(1.0, 0.0, 1.0);
  214.   glPointSize(2);
  215.   glBegin(GL_POINTS);
  216.   for (i = 0; i < VMAJOR_ORDER; i++) {
  217.     for (j = 0; j < VMINOR_ORDER; j++) {
  218.       glVertex4dv(&point2[i * 4 * VMINOR_ORDER + j * 4]);
  219.     }
  220.   }
  221.   glEnd();
  222. }
  223.  
  224. static void
  225. DrawMapEval1(float du)
  226. {
  227.   float u;
  228.  
  229.   glColor3f(1.0, 0.0, 0.0);
  230.   glBegin(GL_LINE_STRIP);
  231.   for (u = 0.0; u < 1.0; u += du) {
  232.     glEvalCoord1d(u);
  233.   }
  234.   glEvalCoord1d(1.0);
  235.   glEnd();
  236. }
  237.  
  238. static void
  239. DrawMapEval2(float du, float dv)
  240. {
  241.   float u, v, tmp;
  242.  
  243.   glColor3f(1.0, 0.0, 0.0);
  244.   for (v = 0.0; v < 1.0; v += dv) {
  245.     glBegin(GL_QUAD_STRIP);
  246.     for (u = 0.0; u <= 1.0; u += du) {
  247.       glEvalCoord2d(u, v);
  248.       tmp = (v + dv < 1.0) ? (v + dv) : 1.0;
  249.       glEvalCoord2d(u, tmp);
  250.     }
  251.     glEvalCoord2d(1.0, v);
  252.     glEvalCoord2d(1.0, v + dv);
  253.     glEnd();
  254.   }
  255. }
  256.  
  257. static void
  258. RenderEval(void)
  259. {
  260.  
  261.   if (colorType) {
  262.     glEnable(GL_MAP1_COLOR_4);
  263.     glEnable(GL_MAP2_COLOR_4);
  264.   } else {
  265.     glDisable(GL_MAP1_COLOR_4);
  266.     glDisable(GL_MAP2_COLOR_4);
  267.   }
  268.  
  269.   if (textureType) {
  270.     glEnable(GL_TEXTURE_2D);
  271.     glEnable(GL_MAP2_TEXTURE_COORD_2);
  272.   } else {
  273.     glDisable(GL_TEXTURE_2D);
  274.     glDisable(GL_MAP2_TEXTURE_COORD_2);
  275.   }
  276.  
  277.   if (polygonFilled) {
  278.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  279.   } else {
  280.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  281.   }
  282.  
  283.   glShadeModel(GL_SMOOTH);
  284.  
  285.   switch (mapType) {
  286.   case EVAL:
  287.     switch (arrayType) {
  288.     case ONE_D:
  289.       glDisable(GL_MAP2_VERTEX_4);
  290.       glEnable(GL_MAP1_VERTEX_4);
  291.       DrawPoints1();
  292.       DrawMapEval1(0.1 / VORDER);
  293.       break;
  294.     case TWO_D:
  295.       glDisable(GL_MAP1_VERTEX_4);
  296.       glEnable(GL_MAP2_VERTEX_4);
  297.       DrawPoints2();
  298.       DrawMapEval2(0.1 / VMAJOR_ORDER, 0.1 / VMINOR_ORDER);
  299.       break;
  300.     }
  301.     break;
  302.   case MESH:
  303.     switch (arrayType) {
  304.     case ONE_D:
  305.       DrawPoints1();
  306.       glDisable(GL_MAP2_VERTEX_4);
  307.       glEnable(GL_MAP1_VERTEX_4);
  308.       glColor3f(0.0, 0.0, 1.0);
  309.       glMapGrid1d(40, 0.0, 1.0);
  310.       if (mapPoint) {
  311.         glPointSize(2);
  312.         glEvalMesh1(GL_POINT, 0, 40);
  313.       } else {
  314.         glEvalMesh1(GL_LINE, 0, 40);
  315.       }
  316.       break;
  317.     case TWO_D:
  318.       DrawPoints2();
  319.       glDisable(GL_MAP1_VERTEX_4);
  320.       glEnable(GL_MAP2_VERTEX_4);
  321.       glColor3f(0.0, 0.0, 1.0);
  322.       glMapGrid2d(20, 0.0, 1.0, 20, 0.0, 1.0);
  323.       if (mapPoint) {
  324.         glPointSize(2);
  325.         glEvalMesh2(GL_POINT, 0, 20, 0, 20);
  326.       } else if (polygonFilled) {
  327.         glEvalMesh2(GL_FILL, 0, 20, 0, 20);
  328.       } else {
  329.         glEvalMesh2(GL_LINE, 0, 20, 0, 20);
  330.       }
  331.       break;
  332.     }
  333.     break;
  334.   }
  335. }
  336.  
  337. static void
  338. Reshape(int width, int height)
  339. {
  340.  
  341.   glViewport(0, 0, width, height);
  342.  
  343.   glMatrixMode(GL_PROJECTION);
  344.   glLoadIdentity();
  345.   glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 10.0);
  346.   glMatrixMode(GL_MODELVIEW);
  347. }
  348.  
  349. static void
  350. Key(unsigned char key, int x, int y)
  351. {
  352.   switch (key) {
  353.   case '1':
  354.     arrayType = ONE_D;
  355.     glDisable(GL_AUTO_NORMAL);
  356.     glutPostRedisplay();
  357.     break;
  358.   case '2':
  359.     arrayType = TWO_D;
  360.     glEnable(GL_AUTO_NORMAL);
  361.     glutPostRedisplay();
  362.     break;
  363.   case '3':
  364.     mapType = EVAL;
  365.     glutPostRedisplay();
  366.     break;
  367.   case '4':
  368.     mapType = MESH;
  369.     glutPostRedisplay();
  370.     break;
  371.   case '5':
  372.     polygonFilled = !polygonFilled;
  373.     glutPostRedisplay();
  374.     break;
  375.   case '6':
  376.     mapPoint = !mapPoint;
  377.     glutPostRedisplay();
  378.     break;
  379.   case '7':
  380.     colorType = !colorType;
  381.     glutPostRedisplay();
  382.     break;
  383.   case '8':
  384.     textureType = !textureType;
  385.     glutPostRedisplay();
  386.     break;
  387.   case '9':
  388.     lighting = !lighting;
  389.     if (lighting) {
  390.       glEnable(GL_LIGHTING);
  391.       glEnable(GL_LIGHT0);
  392.       if (arrayType == TWO_D) {
  393.         glEnable(GL_AUTO_NORMAL);
  394.       } else {
  395.         glDisable(GL_AUTO_NORMAL);
  396.       }
  397.     } else {
  398.       glDisable(GL_LIGHTING);
  399.       glDisable(GL_LIGHT0);
  400.       glDisable(GL_AUTO_NORMAL);
  401.     }
  402.     glutPostRedisplay();
  403.     break;
  404.   case 27:             /* Escape key. */
  405.     exit(0);
  406.   }
  407. }
  408.  
  409. static void
  410. Menu(int value)
  411. {
  412.   /* Menu items have key values assigned to them.  Just pass
  413.      this value to the key routine. */
  414.   Key(value, 0, 0);
  415. }
  416.  
  417. static void
  418. SpecialKey(int key, int x, int y)
  419. {
  420.  
  421.   switch (key) {
  422.   case GLUT_KEY_LEFT:
  423.     rotY -= 30;
  424.     glutPostRedisplay();
  425.     break;
  426.   case GLUT_KEY_RIGHT:
  427.     rotY += 30;
  428.     glutPostRedisplay();
  429.     break;
  430.   case GLUT_KEY_UP:
  431.     rotX -= 30;
  432.     glutPostRedisplay();
  433.     break;
  434.   case GLUT_KEY_DOWN:
  435.     rotX += 30;
  436.     glutPostRedisplay();
  437.     break;
  438.   }
  439. }
  440.  
  441. static void
  442. Draw(void)
  443. {
  444.  
  445.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  446.  
  447.   glPushMatrix();
  448.  
  449.   glTranslatef(0.0, 0.0, translateZ);
  450.   glRotatef(rotX, 1, 0, 0);
  451.   glRotatef(rotY, 0, 1, 0);
  452.   RenderEval();
  453.  
  454.   glPopMatrix();
  455.  
  456.   if (doubleBuffer) {
  457.     glutSwapBuffers();
  458.   } else {
  459.     glFlush();
  460.   }
  461. }
  462.  
  463. static void
  464. Args(int argc, char **argv)
  465. {
  466.   GLint i;
  467.  
  468.   doubleBuffer = GL_FALSE;
  469.  
  470.   for (i = 1; i < argc; i++) {
  471.     if (strcmp(argv[i], "-sb") == 0) {
  472.       doubleBuffer = GL_FALSE;
  473.     } else if (strcmp(argv[i], "-db") == 0) {
  474.       doubleBuffer = GL_TRUE;
  475.     }
  476.   }
  477. }
  478.  
  479. int
  480. main(int argc, char **argv)
  481. {
  482.   GLenum type;
  483.  
  484.   glutInit(&argc, argv);
  485.   Args(argc, argv);
  486.  
  487.   type = GLUT_RGB | GLUT_DEPTH;
  488.   type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  489.   glutInitDisplayMode(type);
  490.   glutInitWindowSize(300, 300);
  491.   glutCreateWindow("Evaluator Test");
  492.  
  493.   glutCreateMenu(Menu);
  494.   glutAddMenuEntry("One dimensional", '1');
  495.   glutAddMenuEntry("Two dimensional", '2');
  496.   glutAddMenuEntry("Eval map type", '3');
  497.   glutAddMenuEntry("Mesh map type", '4');
  498.   glutAddMenuEntry("Toggle filled", '5');
  499.   glutAddMenuEntry("Toggle map point", '6');
  500.   glutAddMenuEntry("Toggle color", '7');
  501.   glutAddMenuEntry("Toggle texture", '8');
  502.   glutAddMenuEntry("Toggle lighting", '9');
  503.   glutAddMenuEntry("Quit", 27);
  504.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  505.   glutAttachMenu(GLUT_LEFT_BUTTON);
  506.  
  507.   Init();
  508.  
  509.   glutReshapeFunc(Reshape);
  510.   glutKeyboardFunc(Key);
  511.   glutSpecialFunc(SpecialKey);
  512.   glutDisplayFunc(Draw);
  513.   glutMainLoop();
  514.   return 0;
  515. }
  516.